package com.qsl.ssyx.cart.api;

import com.qsl.ssyx.cart.service.CartService;
import com.qsl.ssyx.client.activity.ActivityFeignClient;
import com.qsl.ssyx.common.Interceptor.AuthContextHolder;
import com.qsl.ssyx.common.result.Result;
import com.qsl.ssyx.model.order.CartInfo;
import com.qsl.ssyx.vo.order.OrderConfirmVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

// Redis使用Hash表存储购物车信息，对应Map<String, Map<String, String>>
@Api(tags = "微信小程序-购物车接口")
@RestController
@RequestMapping("/api/cart")
public class CartApiController {

    @Autowired
    private CartService cartService;
    @Autowired
    private ActivityFeignClient activityFeignClient;

    @ApiOperation("根据用户Id获取-购物车商品列表")
    @GetMapping("cartList")
    public Result getCartList() {
//        Long userId = AuthContextHolder.getUserId();
        Long userId = 1L;
        List<CartInfo> cartInfoList = cartService.getCartList(userId);
        return Result.ok(cartInfoList);
    }

    @ApiOperation("查询购物车商品列表-带优惠劵")
    @GetMapping("activityCartList")
    public Result activityCartList() {
        //  获取用户id
//        Long userId = AuthContextHolder.getUserId();
        Long userId = 1L;

        // 获取购物车商品信息
        List<CartInfo> cartInfoList = cartService.getCartList(userId);
        System.out.println("=====>cartInfoList: " + cartInfoList);

        OrderConfirmVo orderConfirmVo = activityFeignClient.findCartActivityAndCoupon(cartInfoList, userId);
        return Result.ok(orderConfirmVo);
    }


    @ApiOperation("添加商品进购物车")
    @GetMapping("addToCart/{skuId}/{skuNum}")
    public Result addToCart(@ApiParam(value = "商品id", required = true) @PathVariable("skuId") Long skuId,
                            @ApiParam(value = "商品数量", required = true) @PathVariable("skuNum") Integer skuNum) {

        // TODO 添加商品进购物车-获取用户id
        // 获取用户id
//        Long userId = AuthContextHolder.getUserId();
        Long userId = 1L;
        cartService.addToCart(userId, skuId, skuNum);
        return Result.ok(null);
    }


    @ApiOperation("根据商品Id删除购物车商品")
    @DeleteMapping("deleteCart/{skuId}")
    public Result deleteCartBySkuId(@ApiParam(value = "商品id", required = true) @PathVariable Long skuId) {
//        Long userId = AuthContextHolder.getUserId();
        Long userId = 1L;

        cartService.deleteCartBySkuId(skuId, userId);
        return Result.ok(null);
    }

    @ApiOperation("清空购物车所有商品-根据用户Id删除")
    @DeleteMapping("deleteAllCart")
    public Result deleteAllCart() {
//        Long userId = AuthContextHolder.getUserId();
        Long userId = 1L;

        cartService.deleteAllCart(userId);
        return Result.ok(null);
    }

    @ApiOperation("根据用户Id清空购物车商品-批量删除")
    @DeleteMapping("batchDeleteCart")
    public Result batchDeleteCart(@ApiParam(value = "商品Ids", required = true) @RequestBody List<Long> skuIdList) {
//        Long userId = AuthContextHolder.getUserId();
        Long userId = 1L;

        cartService.batchDeleteCart(userId, skuIdList);
        return Result.ok(null);
    }

    @ApiOperation("选择购物车指定商品-根据用户Id获取")
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public Result checkCart(@ApiParam(value = "商品Id", required = true) @PathVariable Long skuId,
                            @ApiParam(value = "是否选中", required = true) @PathVariable Integer isChecked) {
        //        Long userId = AuthContextHolder.getUserId();
        Long userId = 1L;
        // 更新Redis购物车商品选中状态
        cartService.checkCart(userId, isChecked, skuId);
        return Result.ok(null);
    }

    @ApiOperation("全选/全不选购物车商品-根据用户Id")
    @GetMapping("checkAllCart/{isChecked}")
    public Result checkAllCart(@ApiParam(value = "是否选中", required = true) @PathVariable Integer isChecked) {
        //        Long userId = AuthContextHolder.getUserId();
        Long userId = 1L;
        // 修改Redis购物车商品选中状态-全选/全不选
        boolean flag = cartService.checkAllCart(userId, isChecked);
        return Result.ok(flag);
    }

    @ApiOperation("批量选中购物车商品-根据用户Id")
    @PostMapping("batchCheckCart/{isChecked}")
    public Result batchCheckCart(@ApiParam(value = "是否选中", required = true) @PathVariable Integer isChecked,
                                 @ApiParam(value = "是否选中", required = true) @RequestBody List<Long> skuIdList) {
        //        Long userId = AuthContextHolder.getUserId();
        Long userId = 1L;
        // 修改Redis购物车商品选中状态-批量选中
        boolean flag = cartService.batchCheckCart(skuIdList, userId, isChecked);
        return Result.ok(flag);
    }

    @ApiOperation("获取购物车里选中的商品-根据用户Id")
    @GetMapping("inner/getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@ApiParam(value = "用户Id", required = true) @PathVariable Long userId) {
        List<CartInfo> cartInfoList = cartService.getCartCheckedList(userId);
        return cartInfoList;
    }

    @ApiOperation("根据用户id,获取当前用户选中购物车选中选中的商品")
    @GetMapping("inner/getCartListByUserId/{userId}")
    public List<CartInfo> getCartCheckedListByUserId(@ApiParam(value = "用户Id", required = true) @PathVariable Long userId) {
        List<CartInfo> cartInfoList = cartService.getCartListByUserId(userId);
        return cartInfoList;
    }

}
